<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2111 – ksh does not allow `function` keyword and `()` at the same time.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2111 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2111">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="ksh-does-not-allow-function-keyword-and--at-the-same-time">ksh
does not allow <code>function</code> keyword and <code>()</code> at the
same time.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2111.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/ksh</span></span>
<span id="cb1-2"><a href="SC2111.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span><span class="fu"> foo()</span> <span class="kw">{</span></span>
<span id="cb1-3"><a href="SC2111.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Hello World&quot;</span></span>
<span id="cb1-4"><a href="SC2111.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2111.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># POSIX sh function</span></span>
<span id="cb2-2"><a href="SC2111.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">foo()</span> <span class="kw">{</span></span>
<span id="cb2-3"><a href="SC2111.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Hello World&quot;</span></span>
<span id="cb2-4"><a href="SC2111.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2111.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ksh extended function</span></span>
<span id="cb3-2"><a href="SC2111.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span><span class="fu"> foo</span> <span class="kw">{</span></span>
<span id="cb3-3"><a href="SC2111.html#cb3-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Hello World&quot;</span></span>
<span id="cb3-4"><a href="SC2111.html#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>Ksh allows two ways of defining functions: POSIX sh style
<code>foo() { ..; }</code> and Ksh specific
<code>function foo { ..; }</code>.</p>
<p>ShellCheck found a function definition that uses both at the same
time, <code>function foo() { ..; }</code> which is not allowed. Use one
or the other.</p>
<p>Note that the two are not identical, for example:</p>
<ul>
<li>Using <code>typeset</code> in a <code>function foo</code> will
create a local variable, while in <code>foo()</code> it will create a
global variable.</li>
<li><code>function foo</code> has its own trap context, while
<code>foo()</code> shares them with the current process.</li>
<li><code>function foo</code> will set <code>$0</code> to foo, while
<code>foo()</code> will inherit <code>$0</code> from the current
process.</li>
</ul>
<h3 id="exceptions">Exceptions:</h3>
<p>In Bash, <code>function foo() { ..; }</code> is allowed, and
<code>function foo</code> and <code>foo()</code> are identical. This
warning does not trigger when the shebang is e.g.
<code>#!/bin/bash</code>.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li>Help by adding links to BashFAQ, StackOverflow, man pages, POSIX,
etc!</li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


